package com.java.io.character_stream;

import java.io.*;

/**
 * 通过缓冲区复制一个文件。
 */
public class BufferedCopyText {

    /**
     *
     * @param orgPath 源文件路径
     * @param desPath 目的地文件路径
     * @param orgFileName 源文件名
     * @param desFileName 目的文件名
     */
    public void copyText(String orgPath, String desPath, String orgFileName, String desFileName) {
        BufferedReader bufr = null;
        BufferedWriter bufw = null;
        try {
            bufr = new BufferedReader(new FileReader(orgPath + orgFileName));
            bufw = new BufferedWriter(new FileWriter(desPath + desFileName));
            String line = null;
            while ((line = bufr.readLine()) != null) {
                bufw.write(line);
                bufw.newLine();
                bufw.flush();
            }
        } catch (IOException e) {
            throw new RuntimeException("读写失败");
        } finally {
            try {
                if (bufr != null)
                    bufr.close();
            } catch (IOException e) {
                throw new RuntimeException("读取关闭失败");
            }
            try {
                if (bufw != null)
                    bufw.close();
            } catch (IOException e) {
                throw new RuntimeException("写入关闭失败");
            }
        }
    }
}
